#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using System.Collections;
using System.Web;
using WaveMaster.Holidays.Club.Domain;
using WaveMaster.Holidays.Club.Service;

#endregion

namespace WaveMaster.Holidays.Club.Web
{
    public partial class UserControlManageTrip : UserControl
    {
        User m_current_user;
        TripService m_TripService;
        SeaPortService m_seaportService;
        VesselService m_vesselService;
        Keys m_currentKey;
        MainForm m_mainForm;
        //EditMode m_editmode;

        public UserControlManageTrip(string name, User currentUser, MainForm mainForm)
        {
            InitializeComponent();
            this.Name = name;
            m_current_user = currentUser;
            m_TripService = new TripService();// (TripService)ServiceFactory.Instance().GetService(ServiceFactory.TRIP_SERVICE);
            m_seaportService = new SeaPortService();// (SeaPortService)ServiceFactory.Instance().GetService(ServiceFactory.SEAPORT_SERVICE);
            m_vesselService = new VesselService();// (VesselService)ServiceFactory.Instance().GetService(ServiceFactory.VESSEL_SERVICE);
            m_mainForm = mainForm;
            setPermission();
            textBoxSearch.SelectAll();
            //LoadAllTrip();
            buttonFind_Click(null, null);
            initToolBarButtonClick();
            enableButton(false);
        }


        private void initToolBarButtonClick()
        {
            toolBarButtonClear.Click += new EventHandler(toolBarButtonClear_Click);
            toolBarButtonDelete.Click += new EventHandler(toolBarButtonDelete_Click);
            toolBarButtonRefresh.Click += new EventHandler(toolBarButtonRefresh_Click);
            toolBarButtonEdit.Click += new EventHandler(toolBarButtonEdit_Click);
            toolBarButtonView.Click += new EventHandler(toolBarButtonView_Click);
        }

        void toolBarButtonView_Click(object sender, EventArgs e)
        {
            loadUserForm(EditMode.Load);
        }

        void toolBarButtonEdit_Click(object sender, EventArgs e)
        {
            loadUserForm(EditMode.Edit);
        }

        void enableButton(bool enable)
        {
            toolBarButtonDelete.Enabled = enable && m_current_user.RoleDetails(this.Name).DeleteFlag;
            toolBarButtonEdit.Enabled = enable && m_current_user.RoleDetails(this.Name).UpdateFlag;
            toolBarButtonView.Enabled = enable && m_current_user.RoleDetails(this.Name).ViewFlag;
        }

        private void setPermission()
        {
            toolBarButtonDelete.Enabled = m_current_user.RoleDetails(this.Name).DeleteFlag;
            toolBarButtonEdit.Enabled = m_current_user.RoleDetails(this.Name).UpdateFlag;
            toolBarButtonClear.Enabled = m_current_user.RoleDetails(this.Name).NewFlag;
            toolBarButtonView.Enabled = m_current_user.RoleDetails(this.Name).ViewFlag;
        }

        void toolBarButtonRefresh_Click(object sender, EventArgs e)
        {
            buttonFind_Click(sender, e);
            //LoadAllTrip();
        }

        void toolBarButtonDelete_Click(object sender, EventArgs e)
        {
            if (listViewRecords.SelectedItem == null) return;
            if (listViewRecords.SelectedItem.Tag == null) return;
            MessageBox.Show("Are you sure to delete this record?", "Confirmation", MessageBoxButtons.YesNo,
                MessageBoxIcon.Question, new EventHandler(DeleteConfirmation));
        }

        void DeleteConfirmation(object sender, EventArgs e)
        {
            try
            {
                MessageBoxWindow msg = (MessageBoxWindow)sender;
                if (msg.DialogResult == DialogResult.Yes)
                {
                    Trip user = (Trip)listViewRecords.SelectedItem.Tag;
                    if (user.m_tripID > 0)
                    {
                        m_TripService.DeleteTrip(user);
                        MessageBox.Show("Record has been deleted.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                       // clearForm();
                        LoadAllTrip();
                        //setEditMode(EditMode.Deleted);
                    }
                }
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        void toolBarButtonClear_Click(object sender, EventArgs e)
        {
            TripForm frm = new TripForm(this.Name, new Trip(), m_current_user, m_mainForm, this, EditMode.Create);
            frm.ShowDialog();
        }

        public void LoadAllTrip()
        {
            buttonFind_Click(null, null);
            //listViewRecords.Items.Clear();
            //IList users = m_TripService.LoadAllTrip();
            //foreach (Trip user in users)
            //{
            //    user.m_vessel = m_vesselService.GetVesselByID(user.m_vessel.m_vesselID);
            //    user.m_seaPort = m_seaportService.GetSeaPortByID(user.m_seaPort.m_seaPortID);
            //    ListViewItem item = listViewRecords.Items.Add(user.m_tripCode);
            //    item.SubItems.Add(user.m_vessel.m_vesselName);
            //    item.SubItems.Add(user.m_seaPort.m_seaPortName);
            //    item.SubItems.Add(user.m_departureDate.ToString("dd-MMM-yyyy"));
            //    item.SubItems.Add(user.m_departureTime.ToString("HH:mm"));
            //    item.SubItems.Add(user.m_avaliable.ToString());
            //    item.Tag = user;
            //}
        }

        private void buttonFind_Click(object sender, EventArgs e)
        {
            listViewRecords.Items.Clear();
            IList users = m_TripService.SearchTripByCodeNameAndDepartureDate(textBoxSearch.Text, dateTimePickerSearch.Value);
            foreach (Trip user in users)
            {
                user.m_vessel = m_vesselService.GetVesselByID(user.m_vessel.m_vesselID);
                user.m_seaPort = m_seaportService.GetSeaPortByID(user.m_seaPort.m_seaPortID);
                ListViewItem item = listViewRecords.Items.Add(user.m_tripCode);
                item.SubItems.Add(user.m_vessel.m_vesselName);
                item.SubItems.Add(user.m_seaPort.m_seaPortName);
                item.SubItems.Add(user.m_departureDate.ToString("dd-MMM-yyyy"));
                item.SubItems.Add(user.m_departureTime.ToString("HH:mm"));
                item.SubItems.Add(user.m_avaliable.ToString());
                item.Tag = user;
            }
            if (users.Count > 1)
            {
                listViewRecords.Focus();
                listViewRecords.SelectedIndex = 0;
            }
            else if (users.Count == 0) 
            {
                ListViewItem item = listViewRecords.Items.Add("Not Found.");
                textBoxSearch.SelectAll(); 
            }
        }

        private void listViewRecords_DoubleClick(object sender, EventArgs e)
        {
            //if (toolBarButtonEdit.Enabled)
            //    loadUserForm(EditMode.Edit);
            //else
            loadUserForm(EditMode.Load);
        }

        private void loadUserForm( EditMode editmode )
        {
            if (listViewRecords.SelectedItem == null) return;
            if (listViewRecords.SelectedItem.Tag == null) return;
            Trip user = (Trip)listViewRecords.SelectedItem.Tag;
            TripForm frm = new TripForm(this.Name, user, m_current_user, m_mainForm, this, editmode);
            frm.ShowDialog();
        }

        private void textBox_KeyDown(object objSender, KeyEventArgs objArgs)
        {
            m_currentKey = objArgs.KeyData;
        }


        private void textBox_TextChanged(object sender, EventArgs e)
        {
            if (m_currentKey == Keys.Enter)
            {
                buttonFind_Click(null, null);
            }
        }

        private void listViewRecords_KeyDown(object objSender, KeyEventArgs objArgs)
        {
            if (objArgs.KeyCode == Keys.Enter)
            {
                listViewRecords_DoubleClick(objSender, null);
            }
        }

        private void textBoxSearch_GotFocus(object sender, EventArgs e)
        {
            textBoxSearch.SelectAll();
        }

        private void listViewRecords_SelectedIndexChanged(object sender, EventArgs e)
        {
            enableButton(listViewRecords.SelectedItem != null);            
            if (listViewRecords.SelectedItem != null)
                enableButton(listViewRecords.SelectedItem.Tag != null);
        }

    }
}